package bm.com.framework.common.unit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.Date;

public class DateUnit {

    /**
     * 时间戳转化日期
     */
    public static String timeToDate(Long timestamp, String pattern) {
        return new SimpleDateFormat(pattern).format(new Date(timestamp));
    }

    /**
     * 日期 转化 时间戳
     */
    public static long dateToTime(String date, String pattern) throws ParseException {
        Date parse = new SimpleDateFormat(pattern).parse(date);
        return parse.getTime();
    }

    /**
     * 获取当前时间戳（秒）
     */
    public static long getNowTimeSecond() {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * 获取当前时间戳（毫秒）
     */
    public static long getNowTimeMilliSecond() {
        return System.currentTimeMillis();
    }

    /**
     * 获取某天秒数时间戳
     *
     * @return
     */
    public static long getDateZeroSecond(int second) {
        LocalDateTime dateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT);
        long timestamp = dateTime.toEpochSecond(ZoneOffset.of("+8"));
        if (second > 0) {
            timestamp = timestamp + second;
        }
        return timestamp;
    }

}
